package ferus.tigris.lines.levels;

import java.util.Random;

import ferus.tigris.lines.GameManager;
import ferus.tigris.lines.Matrix;
import ferus.tigris.lines.Builders.AbstractBehaviorBuilder;
import ferus.tigris.lines.personages.AbstractBehavior;

public class LevelWithRandomInit extends StaticLevel {
	private GameManager gameManager;
	private LevelManager levelManager;

	public LevelWithRandomInit(LevelManager levelManager, GameManager gameManager, int scope, int complexity) {
		super(levelManager, gameManager, scope, complexity);
		this.gameManager = gameManager;
		this.levelManager = levelManager;
	}

	public AbstractLevel nextLevel() {
		return new LevelWithNullInit(levelManager, gameManager, scope(), getNextComplexity());
	}

	public AbstractLevel clone() {
		return new LevelWithRandomInit(levelManager, gameManager, initScope(), complexity());
	}

	public void start() {
		super.start();
		Matrix m = marks();
		
		Random rnd = new Random();
		int x = rnd.nextInt(m.columns()/2);
		int y = rnd.nextInt(m.rows()/2);

		AbstractBehaviorBuilder builder = new AbstractBehaviorBuilder();

		AddRandomMark(builder, x, y);
		AddRandomMark(builder, x, m.rows() - y - 1);
		AddRandomMark(builder, m.columns() - x - 1, y);
		AddRandomMark(builder, m.columns() - x - 1, m.rows() - y - 1);
	}

	protected void AddRandomMark(AbstractBehaviorBuilder builder, int x, int y) {
		Matrix m = marks();
		AbstractBehavior mark = builder.createRandomMark(gameManager.view());
		m.insertMark(x, y, mark);
	}

}
